package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.tomcat.jni.Stdlib;

import entity.Course;
import entity.Grade;
import entity.Student;
import entity.TbClass;
import service.CourseService;
import service.GradeService;
import service.StudentService;
import service.TbClassService;



/**
 * Servlet implementation class GradeServlet
 */
@WebServlet("/grade")
public class GradeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private CourseService courseService=new CourseService();
	private StudentService studentService=new StudentService();
	private TbClassService tbClassService=new TbClassService();
	private GradeService gradeService=new GradeService();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method=request.getParameter("method");
		if("personGrade".contentEquals(method)) {
			personGrade(request,response);
		}else if("getStu".contentEquals(method)) {
			getStu(request,response);
		}else if("getPersonGrade".contentEquals(method)) {
			getPersonGrade(request,response);
		}else if("cjChange".contentEquals(method)) {
			cjChange(request,response);
		}else if("ClassGrade".contentEquals(method)) {
			ClassGrade(request,response);
		}else if("getClassGrade".contentEquals(method)) {
			getClassGrade(request,response);
		}else if("allStudentGrade".contentEquals(method)) {
			allStudentGrade(request,response);
		}else if("bjChange".contentEquals(method)) {
			bjChange(request,response);
		}else if("selfGrade".contentEquals(method)) {
			selfGrade(request,response);
		}else if("selfClassGrade".contentEquals(method)) {
			selfClassGrade(request,response);
		}
	}
	
	private void selfClassGrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Student self = (Student) session.getAttribute("user");
		Integer bjId = self.getTbclass().getId();
		
		List<Course> courses=gradeService.findCourseByBjId(bjId);
		List<List<Object>> cjb= gradeService.getCjb(bjId,courses);
		request.setAttribute("kcs", courses);
		request.setAttribute("cjb", cjb);
		request.getRequestDispatcher("jsp/grade/cjb2.jsp").forward(request, response);		
	}
	private void selfGrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		Student self = (Student) session.getAttribute("user");
		List<Grade> list = gradeService.findByStudentId(self.getId());
		request.setAttribute("list", list);
		request.getRequestDispatcher("jsp/grade/selfGrade.jsp").forward(request, response);
	}
	private void bjChange(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer bjId=Integer.valueOf(request.getParameter("bjId"));
		List<Course> courses=gradeService.findCourseByBjId(bjId);
		List<List<Object>> cjb= gradeService.getCjb(bjId,courses);
		request.setAttribute("kcs", courses);
		request.setAttribute("cjb", cjb);	
		request.getRequestDispatcher("jsp/grade/cjb.jsp").forward(request, response);
		
		
	}
	private void allStudentGrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<TbClass> list = tbClassService.findAll();
		request.setAttribute("list", list);
		request.getRequestDispatcher("jsp/grade/allStudentGrade.jsp").forward(request, response);
	}
	private void getClassGrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 
		Integer bjId =Integer.valueOf(request.getParameter("bjId"));
		Integer kcId =Integer.valueOf(request.getParameter("kcId"));
		List<Student> stus = studentService.findByTbClassId(bjId);
		List<Grade> grades=new ArrayList<Grade>();
		for(Student student:stus) {
			Grade grade=gradeService.findByStudentIdAndCourseId(student.getId(),kcId);
			if(grade==null) {
				grade=new Grade(null, studentService.findById(student.getId()), courseService.findById(kcId), null);
				gradeService.save(grade);
			}
			grades.add(grade);
		}
		request.setAttribute("grades", grades);
		request.getRequestDispatcher("jsp/grade/getClassGrade.jsp").forward(request, response);
		
	}
	private void ClassGrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<TbClass> bjs=tbClassService.findAll();
		List<Course> kcs=courseService.findAll();
		request.setAttribute("bjs", bjs);
		request.setAttribute("kcs", kcs);
		request.getRequestDispatcher("jsp/grade/ClassGrade.jsp").forward(request, response);
		
	}
	private void cjChange(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Integer id=Integer.valueOf(request.getParameter("id"));
		Integer cj=Integer.valueOf(request.getParameter("grade"));
		Grade grade = gradeService.findById(id);
		grade.setGrade(cj);
		gradeService.save(grade);
		PrintWriter writer = response.getWriter();
	}
	private void getPersonGrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer stuId=Integer.valueOf(request.getParameter("stuId"));
		List<Grade> list=gradeService.findByStudentId(stuId);
		request.setAttribute("list", list);
		request.getRequestDispatcher("jsp/grade/getPersonGrade.jsp").forward(request, response);
		
	}
	private void getStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer bjId=Integer.valueOf(request.getParameter("bjId"));
		List<Student> list = studentService.findByTbClassId(bjId);
		request.setAttribute("list", list);
		request.getRequestDispatcher("jsp/grade/getStu.jsp").forward(request, response);
		
	}
	private void personGrade(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<TbClass> list = tbClassService.findAll();
		request.setAttribute("list", list);
		request.getRequestDispatcher("jsp/grade/personGrade.jsp").forward(request,response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}	

}
